<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="userName">
              <el-input v-model="filters.userName" clearable placeholder="请输入名字"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <div>
            <el-tooltip content="刷新" placement="top">
              <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
            </el-tooltip>
            <el-tooltip content="返回" placement="top">
              <el-button circle icon="fa fa-mail-forward" @click="returnList" size="mini"></el-button>
            </el-tooltip>
          </div>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click.native="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row ref="tableRow">
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column type="index" label="序号" align="center" width="100"></el-table-column>
        <el-table-column prop="boxName" label="设备名称"></el-table-column>
        <el-table-column prop="userName" label="用户名"></el-table-column>
        <el-table-column prop="fid" label="指纹ID" align="center"></el-table-column>
        <el-table-column prop="fidType" label="指纹类型" align="center"></el-table-column>
        <el-table-column prop="fid" :formatter="formatFid" label="是否录入指纹" align="center"></el-table-column>
        <el-table-column prop="fidStatus" :formatter="formatState" label="指纹状态" align="center"></el-table-column>
        <el-table-column prop="createUserName" label="创建人" align="center"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-has="permissionEdit" @click="handleClickEditor(scope.row)" size="mini">编辑</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <!-- <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item> -->
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
                <el-dropdown-item v-if="!scope.row.fid" @click.native="handleClickCMD(scope.row, 110)">新增指纹</el-dropdown-item>
                <el-dropdown-item v-if="scope.row.fid" @click.native="handleClickCMD(scope.row, 111)">删除指纹</el-dropdown-item>
                <el-dropdown-item v-if="scope.row.fidStatus == 1" @click.native="handleClickCMD(scope.row, 114)">禁用指纹</el-dropdown-item>
                <el-dropdown-item v-if="scope.row.fidStatus == 0" @click.native="handleClickCMD(scope.row, 115)">启用指纹</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <BoxFingerprintDrawer ref="boxFingerprintDrawer"></BoxFingerprintDrawer>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import EquipApi from '@/api/equip/equip';
import BoxFingerprintDrawer from '@/components/BoxFingerprintDrawer';

export default {
  name: 'boxFingerprintManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      boxId: '',
      filters: {
        userName: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },


      permissionAdd: '',
      permissionCheck: '',
      permissionEdit: '',
      permissionDelete: '',
    };
  },
  components: {
    BoxFingerprintDrawer,
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.listData();
      this.getTableHeight();
      if (this.$route.query.id) {
        this.boxId = this.$route.query.id;
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        // 浏览器尺寸变化响应事件
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    onSearch() {
      this.listData();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    listData() {
      var cond = {
        boxId: this.$route.query.id,
        userName: this.filters.userName,
      };
      EquipApi.listEquipBoxUser(cond).then((res) => {
        console.log(res);
        this.dataList = res.data;
      });
    },
    // 列表数据修改
    handleClickEditor(row) {
      this.$refs.boxFingerprintDrawer.handleChange(row);
    },
    handleClickDelete(val) {
      this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        EquipApi.deleteEquipBoxUser({
          id: val.boxUserId,
          state: -1,
        }).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickAdd() {
      this.$refs.boxFingerprintDrawer.handleCreate(this.boxId);
    },
    handleClickCMD(row, val) {
      var tit = '';
      if (val == 110) {
        tit = '确认为该用户新增指纹？';
      } else if (val == 111) {
        tit = '确认删除用户该指纹？';
      } else if (val == 114) {
        tit = '确认禁用用户该指纹？';
      } else if (val == 115) {
        tit = '确认启用用户该指纹？';
      }
      var cond = {
        id: row.boxUserId,
        cmd: val,
      };
      this.$confirm(tit, message.TITLE_PROMPT, {}).then(() => {
        EquipApi.changeEquipBoxUserCmd(cond).then((res) => {
          console.log(res);
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },

    // 过滤器
    formatFid(row, colum, cellValue) {
      if (cellValue) {
        return '否';
      } else {
        return '是';
      }
    },
    formatState(row, colum, cellValue) {
      if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      }
    },

    returnList() {
      this.dataForm = [];
      this.$store.dispatch('delView', this.$route);
      this.$router.push({ path: '/equip/BoxManagement' });
    },
  },
};
</script>

<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
